<template>
    
	<Form ref="loginForm" :model="form" :rules="rules">
        <Spin size="large" fix v-if="spinShow"></Spin>
		<FormItem label="选择教职工" prop="user_id">
			<Select v-model="form.user_id" style="width:80%" filterable>
	        <Option v-for="item in teacher" :value="item.user_id" :key="item.user_id">
	          {{item.teacher_name.zh?item.teacher_name.zh:item.teacher_name.mn}}
	        </Option>
	      </Select>
		</FormItem>
		<FormItem label="选择课程" prop="course_id">
			<Select v-model="form.course_id" multiple style="width:80%" v-if="dataId == ''">
                <Option v-for="item in courseList" :value="item.id" :key="item.id">
                {{item.course_name.zh?item.course_name.zh:item.course_name.mn}}
                </Option>
            </Select>
            <Select v-model="formInine.course_id" style="width:80%" v-else>
                <Option v-for="item in courseList" :value="item.id" :key="item.id">
                {{item.course_name.zh?item.course_name.zh:item.course_name.mn}}
                </Option>
            </Select>
		</FormItem>
	
		<FormItem>
			<Button type="primary" @click="handleSubmit('loginForm')" :loading="loadingbtn">{{btn}}</Button>	
			<Button type="ghost" style="margin-left: 8px" @click="handleReset('loginForm')">重置</Button>
		</FormItem>
	</Form>
</template>

<script>
	export default {
		props: ["dataId"],
		data() {
			return {
                url: "api/teacher/v1/teachercourse/edit/",
                addUrl:'api/teacher/v1/teachercourse/add',
                loadingbtn:false,
                spinShow:true,
				teacher: [],
                courseList: [],
                formInine:{
                    user_id:'',
                    course_id:'',
                },
                btn:'',
				form: {
					user_id: "",
					course_id: [],
				},
				rules: {
					// course_id: [{
					// 	type: "number",
					// 	required: true,
					// 	message: "请选择课程",
					// 	trigger: "change"
					// }],
					user_id: [{
						type: 'number',
						required: true,
						message: "请选择教师",
						trigger: "change"
					}],
	
				}
			};
		},
		created() {
			this.selectdeperment();
	
            this.course()
			if(this.dataId !=''){
                this.findData()
                this.btn='修改'
            }else{
                this.btn='添加'
            }
		},
		methods: {
			selectdeperment() {
				this.$http.get("api/teacher/v1/get/teacher/listname").then(val => {
                    this.teacher = val.data;
                    this.spinShow=false;
				});
			},
			course() {
				this.$http.get("api/base/v1/get/course/list").then(val => {
					this.courseList = val.data;
				});
			},
	        findData(){
                this.$http
				.get("api/teacher/v1/teachercourse/find/" + this.dataId)
				.then(val => {
					this.form.user_id = (val.data.user_id);
                    this.formInine.course_id = parseInt(val.data.course_id);
                    
				});
            },
			handleSubmit(name) {
                this.loadingbtn = true
				this.$refs[name].validate(valid => {
					if (valid) {
                        if(this.dataId !=''){
                            this.formInine.user_id=this.form.user_id
                            this.$http.post(this.url + this.dataId, this.formInine).then(
							val => {
	
								this.$Message.success(this.$t('edit_success'));
                                this.$emit("requery", true);
                                this.loadingbtn=false
	
							}).catch(err =>{
                                this.$Message.error(this.$t('edit_error'))
                                this.loadingbtn=false
                            });
                        }else{
                            this.$http.post(this.addUrl, this.form).then(
							val => {
								this.$Message.success(this.$t('add_success'));
                                this.$emit("requery", true);
                                this.loadingbtn=false								
							}).catch(err =>{
                                this.$Message.error(this.$t('add_error'))
                                this.loadingbtn=false
                            });
                        }
						
					} else {
						this.$Message.error(this.$t('Please_fill_in_the_complete'));
						this.loadingbtn=false		
					}
				});
			},
			handleReset(name) {
				this.$refs[name].resetFields();
			}
		}
	};
</script>


